package com.wh.springboot.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import com.wh.springboot.common.Result;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.wh.springboot.service.ICovidNewsService;
import com.wh.springboot.common.entity.CovidNews;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author elsy
 * @since 2023-04-19
 */
@RestController
@RequestMapping("/covidnews")
public class CovidNewsController {
    @Resource
    private ICovidNewsService covidNewsService;

    // 新增和修改
    @PostMapping
    public Result save(@RequestBody CovidNews covidNews) {
        //新增或者更新
        return Result.success( covidNewsService.saveOrUpdate(covidNews));
    }

    //  查询所有数据
    @GetMapping
    public Result findAll() {
        return Result.success(covidNewsService.list());
    }

    //根据id查询
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(covidNewsService.getById(id));
    }

    //删除数据
    @DeleteMapping("/{id}")//id 与下面的id必须一致
    //    @PathVariable 表示 url 这个参数
    public Result delete(@PathVariable Integer id) {
        return Result.success(covidNewsService.removeById(id));
    }

    //批量删除
    @DeleteMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids){
        return Result.success(covidNewsService.removeByIds(ids));
    }

    //分页查询
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                                    @RequestParam Integer pageSize,
                           @RequestParam(defaultValue = "") String title
    ) {
        QueryWrapper<CovidNews> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        if (!"".equals(title)) {
            queryWrapper.like("title", title);
        }
        return Result.success(covidNewsService.page(new Page<>(pageNum, pageSize),queryWrapper));
    }
}

